/**
 * Created by zengy on 2017/9/4.
 */
angular.module('mainApp.channel',['ui.router'])
    .config(function ($stateProvider) {
        $stateProvider
            .state({
                name:'channel/channelList',
                url:'/channel/channelList',
                templateUrl:'views/channel/channel_list.html?v='+new Date().getMinutes(),
                controller:'channelCtrl'
            })
    })
.controller('channelCtrl',function ($scope) {
    $scope.$emit('arg',12);
    $scope.channelModel={
        "pageNo":1 ,// 页数（从1开始）
        "pageSize":10 // 每页大小
    };
    $scope.pagination = function (currentPage, pageCount) {
        $('.page-function #pagination').remove();
        $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
        $('.pagination').twbsPagination({
            totalPages: pageCount,
            startPage: currentPage,
            visiblePages: 5,
            first: "首页",
            prev: "上一页",
            next: "下一页",
            last: "尾页",
            href: 'javascript:;',
            //hideOnlyOnePage : true,
            onPageClick: function (event, page) {
                $scope.channel(page);
            }
        });
    };
    $scope.channel=function (page) {
        if(page){
            $scope.channelModel.pageNo=page;
        }
        new HttpRequest().getChannelManagerList($scope.channelModel,function (res) {
            if(res.code=='000'){
                $scope.channelList = res.data.list;
                $scope.channelList.forEach(function (item) {
                    var goodsitem=[];
                    var goodArr=item.goods.split(',');
                    goodArr.map(function (n) {
                        if(n){
                            var obj={};
                            obj.goodsId= n.split(" ")[0];
                            obj.goodsName=n.split(" ")[1];
                            goodsitem.push(obj);
                        }

                    });
                    item.goodsList=goodsitem;

                })
                $scope.$apply();
                if ($scope.channelList.length > 0) {
                    $scope.pagination($scope.channelModel.pageNo, res.data.pageCount);
                }
            } else {
                layer.msg(res.msg)
            }

        })
    }
    $scope.channel($scope.channelModel.pageNo);
    //添加渠道二维码
    $scope.addChannelCode=function () {
        new HttpRequest().addChannel({cellPhone:$scope.cellPhone,channelName:$scope.channelName},function (res) {
            if(res.code =='000'){
                location.reload()
            }else{
                layer.msg(res.msg);
            }
        })

    };
    $scope.resetChannel=function () {
        $scope.cellPhone="";
        $scope.channelName="";
    }
    //获取服务包信息
    new HttpRequest().queryGoodsList(function (res) {
        if(res.code == '000'){
            $scope.goodsList=res.data;
            $scope.$apply();
        }
    });
    //查看所选服务
    $scope.setting=function (item) {
        if(item){
            $scope.currentItem=item;
            var choiceArr=item.goodsList;
            choiceArr.forEach(function (val) {
                $('#goodsList').find('input[type="checkbox"]').each(function () {
                    if($(this).val()==val.goodsId){
                        $(this).prop('checked',true);
                    }
                })
            })
        }
    }
//    提交服务
    $scope.commitSevers=function () {
        var goodsId=[];
       $('#goodsList input:checked').each(function () {
           goodsId.push($(this).val())
       });
        var params={
            "wxUserId":$scope.currentItem.wxUserId, // 微信用户id（推荐人）
            "goodsId":goodsId, // 货物id(数组)
            "cellPhone":$scope.currentItem.cellPhone, // 电话
            "channelName":$scope.currentItem.channelName, // 渠道名称
            "applyIntroduce":"" // 申请说明
        };
        new HttpRequest().setOpenService(params,function (res) {
            if(res.code == '000'){
                location.reload();
            }else{
                layer.msg(res.msg);
            }
        })
    };
//    审核
    $scope.examine=function (item) {
        if(item){
            $scope.currentItem=item;
            $scope.selectedGoodsList=item.goodsList;
        }

    };
//    提交审核
    $scope.examineCommit=function () {
        new HttpRequest().openService({wxRecommendPeopleIds:$scope.currentItem.wxRecommendPeopleIds},function (res) {
            if(res.code == '000'){
                location.reload()
            }else{
                layer.msg(res.msg);
            }
        })
    };
//    下载二维码
    $scope.download=function (item) {
         location.href=baseUrl+'/channelManager/getQRcode?wxRecommendPeopleIds='+item.wxRecommendPeopleIds+'&token='+localStorage.token+'&userId='+localStorage.userId+'&pixel=200';
    };
//    禁用
    $scope.forbid=function (item) {
        var index = layer.confirm('是否禁用？', {
            title: '禁用',
            btn: ['确定', '取消'],//按钮
        }, function () {
            new HttpRequest().forbidService({wxRecommendPeopleIds:item.wxRecommendPeopleIds},function (res) {
                if(res.code == '000'){
                    location.reload()
                }else{
                    layer.msg(res.msg);
                }
            })
        }, function () {
            layer.close(index)
        })



    }

    




})